<?php
/**
 * Created by PhpStorm.
 * User: fengzb
 * Date: 2017/11/25
 * Time: 17:03
 */

namespace app\admin\controller;

use app\admin\Controller;
use think\Loader;
use think\Session;
use think\Db;

class Goods extends Controller
{

    /**
     * 商品列表
     * @return string
     */
    public function index()
    {
        $params = $this->request->param();
        $maps = [];
        if(isset($params['goods_name'])&&$params['goods_name']!=''){
            $maps['goods_name'] = array('like','%'.trim($params['goods_name']).'%');
        }

        if(isset($params['status']) && $params['status']!=''){
            $maps['status'] = array('eq',intval($params['status']));
        }

        // 分页查询 每页显示10条
        $goods = Db::table('kyf_goods')->where($maps)->order('goods_id desc')->paginate(10,false,$this->request->param());
        $res = $goods->toArray();
        $pages = $goods->render();

        foreach ($res['data'] as &$val){
            $picarr = explode(',',$val['goods_pics']);
            $val['pic'] = $picarr[0];
            $val['price'] = fmoney($val['price'],false);// 分转成元显示
        }

        $this->view->assign('list',$res['data']);
        $this->view->assign('total',$res['total']);
        $this->view->assign('page',$pages);

        return $this->view->fetch();
    }

    /**
     * 添加商品
     * @return mixed
     */
    public function add()
    {

        if($this->request->isPost()){
            $params = $this->request->param();

            $goods_name = isset($params['goods_name'])?trim($params['goods_name']):'';
            $short_name = isset($params['short_name'])?trim($params['short_name']):'';
            $cate_id = isset($params['cate_id']) ? intval($params['cate_id']) : 0;
            $goodspics = isset($params['goodspic'])&&!empty($params['goodspic'])?implode(',',$params['goodspic']):'';
            $goods_desc = isset($params['goods_desc'])?trim($params['goods_desc']):'';
            $sale_type = isset($params['sale_type'])?trim($params['sale_type']):0;
            $price = isset($params['price'])?trim($params['price']):'';
            $status = isset($params['status'])?trim($params['status']):'';
            $editorValue = isset($params['contents'])?trim($params['contents']):'';

            $data = array();
            $data['goods_name'] = $goods_name;
            $data['short_name'] = $short_name;
            $data['goods_desc'] = $goods_desc;
            $data['buytype'] = $sale_type;
            $data['price'] = fmoney($price);//元转成分存入数据库
            $data['status'] = $status;
            $data['contents'] = $editorValue;
            $data['create_time'] = time();
            $data['cate_id'] = $cate_id;
            $data['goods_pics'] = $goodspics;
            $data['admin_id'] = Session::get('auth_id');

            $goodsid = Db::table('kyf_goods')->insert($data,false,true);
            if($goodsid > 0){
                return ajax_return(array(),'添加成功');
            } else {
                return ajax_return(array(),'提交失败');
            }

        } else {
            $cates = Db::table('kyf_cates')->where('pid!=0')->select();
            $this->view->assign('cates',$cates);
            return $this->view->fetch();
        }
    }


    /**
     * 商品上下架
     */
    public function setStatus(){
        $params = $this->request->param();
        $id  = isset($params['id'])?intval($params['id']):0;
        $status = isset($params['status'])?intval($params['status']):0;
        if($id==0){
            return ajax_return(array(),'参数错误',2);
        }
        $save['last_update_time'] = time();
        $save['admin_id'] = Session::get('auth_id');
        if($status==0){
            $save['status'] = 1;
        } else {
            $save['status'] = 0;
        }

        $srt = Db::table('kyf_goods')->where('goods_id='.$id)->update($save);
        if($srt){
            return ajax_return(array(),'操作成功');
        } else {
            return ajax_return(array(),'操作失败',1);
        }

    }

    /**
     * 修改商品
     */
    public function update(){
        $params = $this->request->param();
        $id  = isset($params['id'])?intval($params['id']):0;
        if($id==0){
            exit('参数错误');
        }

        if($this->request->isPost()){

            $goods_name = isset($params['goods_name'])?trim($params['goods_name']):'';
            $short_name = isset($params['short_name'])?trim($params['short_name']):'';
            $cate_id = isset($params['cate_id']) ? intval($params['cate_id']) : 0;
            $goodspics = isset($params['goodspic'])&&!empty($params['goodspic'])?implode(',',$params['goodspic']):'';
            $goods_desc = isset($params['goods_desc'])?trim($params['goods_desc']):'';
            $sale_type = isset($params['sale_type'])?trim($params['sale_type']):0;
            $price = isset($params['price'])?trim($params['price']):'';
            $status = isset($params['status'])?trim($params['status']):'';
            $editorValue = isset($params['contents'])?trim($params['contents']):'';

            $data = array();
            $data['goods_name'] = $goods_name;
            $data['short_name'] = $short_name;
            $data['goods_desc'] = $goods_desc;
            $data['buytype'] = $sale_type;
            $data['price'] = fmoney($price);//元转成分存入数据库
            $data['status'] = $status;
            $data['contents'] = $editorValue;
            $data['create_time'] = time();
            $data['cate_id'] = $cate_id;
            $data['goods_pics'] = $goodspics;
            $data['admin_id'] = Session::get('auth_id');

            $srt = Db::table('kyf_goods')->where('goods_id='.$id)->update($data);
            if($srt){
                return ajax_return(array(),'修改成功');
            } else {
                return ajax_return(array(),'修改失败');
            }

        } else {

            $cates = Db::table('kyf_cates')->where('pid!=0')->select();
            $goods = Db::table('kyf_goods')->where('goods_id='.$id)->find();
            $goods['pic'] = explode(',',$goods['goods_pics']);
            $goods['price'] = fmoney($goods['price'],false);
            $this->view->assign('cates',$cates);
            $this->view->assign('goods',$goods);
            return $this->view->fetch();
        }

    }

}